<?php
include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');

$resultatmanager = new ResultatManager($connexion);
$testmanager = new TestManager($connexion);
$reponsemanager = new ReponseManager($connexion);
$exercicemanager = new ExerciceManager($connexion);
$questionmanager = new QuestionManager($connexion);

$idEleve = $resultatmanager->get($_POST['id'])->idRefEleve();
$idTest = $resultatmanager->get($_POST['id'])->idRefTest();
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title><?php echo "Rapport de résultat de ".$elevemanager->get($idEleve)->prenom()." ".$elevemanager->get($idEleve)->nom()." du ".date("d/m/Y"); ?></title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="css/font-awesome.min.css">
  <link rel="stylesheet" href="js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="css/bootstrap.min.css">

    <!-- App CSS -->
  <link rel="stylesheet" href="css/target-admin.css">
  <link rel="stylesheet" href="css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>

<body>

<div class="container">

  <div class="content">

    <div class="content-container">
	<br><br>
      
      <div class="row">
        <div style="float:left;">
        <?php echo "Le ".date("d/m/Y")."<br>Prénom : ".$elevemanager->get($idEleve)->prenom()."<br>Nom : ".$elevemanager->get($idEleve)->nom()."<br>Date de naissance : ".date("d/m/Y", $elevemanager->get($idEleve)->datenaissance())."<br>Test : ".$testmanager->get($idTest)->intitule(); ?>
        </div>
        
        <div align="right">14, rue Romarin<br>69001 LYON<br>Tél : 04 78 28 07 01<br>Fax : 04 78 30 17 22<br>accueil@centreorientationscolaire.fr<br></div>     
      </div>
      
		<hr><div class="content-header-title" align="center">Centre d'Orientation Scolaire</div><hr>

<?php 	foreach($resultatmanager->ListExercice($idEleve, $idTest) as $ex)
		{
			$exo = $exercicemanager->get($ex);
			if($exo->idRefExemple() == 0)
			{
				echo "<h4>".$exo->intitule()."</h4>"; ?>        
              <table 
                class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                data-provide="datatable" 
                data-display-rows="10"
                data-info="false"
                data-search="false"
                data-length-change="true"
                data-paginate="false"
              >
                  <tbody>
                  <tr>
                  <td id="legend-<?php echo $ex; ?>"></td>
                  <td><div id="stacked-horizontal-chart-<?php echo $ex; ?>" class="chart-holder" style="height:100px;"></div></td>
                  </tr>
                  </tbody>
                </table>
<?php } } ?>    
        <br><br><table class="table table-striped table-bordered table-hover table-highlight table-checkable"><thead><tr><td style="color:white;">Commentaire</td></tr></thead><tbody><tr><td><?php echo nl2br($_POST[$_POST['id']]); ?></td></tr></tbody></table> 

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->

  <script src="js/libs/jquery-1.10.1.min.js"></script>
  <script src="js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="js/plugins/flot/jquery.flot.js"></script>
  <script src="js/plugins/flot/jquery.flot.orderBars.js"></script>
  <script src="js/plugins/flot/jquery.flot.pie.js"></script>
  <script src="js/plugins/flot/jquery.flot.stack.js"></script>
  <script src="js/plugins/flot/jquery.flot.tooltip.min.js"></script>
  <script src="js/plugins/flot/jquery.flot.resize.js"></script>

  <!-- App JS -->
  <script src="js/target-admin.js"></script>
<?php 	foreach($resultatmanager->ListExercice($idEleve, $idTest) as $ex)
		{
			$exo = $exercicemanager->get($ex);
			if($exo->idRefExemple() == 0)
			{ ?>  
  <!-- Plugin JS -->
  <script type="text/javascript">
	$(function () {
	
		var ds=[], data, chartOptions;		
<?php
	$reponseJuste = 0;
	foreach($resultatmanager->getByExerciceEleve($ex,$idEleve) as $res)
	{
		if($reponsemanager->get($res->idRefReponse())->correcte() == 1){
			$reponseJuste++;
		}
	}
	
	$rapidite = $resultatmanager->getByExerciceEleve($ex,$idEleve)[0]->temps() / $exo->duree_seconde();
	$rapidite *= 100;
	
	$txRapidite = 0;
	foreach($resultatmanager->getByExercice($ex) as $res)
	{
		$txRapidite += $res->temps();
	}
	$txRapidite /= sizeof($resultatmanager->getByExercice($ex));
	$txRapidite /= $exo->duree_seconde();
	$txRapidite *= 100;
	
	$reponseRepondu = sizeof($resultatmanager->getQuestion($ex, $idEleve));

	
	$moyenneRep = number_format($resultatmanager->TxReponseCorrecte($ex),2);
	if($moyenneRep < 50)
	{
		$ecartfortRep = ($moyenneRep * 2) - ($moyenneRep*0.1);
		$ecartfaibleRep = $moyenneRep / 2;
		$ecartmidRep = 100-$ecartfortRep-$ecartfortRep-$moyenneRep;	
	} else {
		$ecartfaibleRep = ($moyenneRep / 2) - ($moyenneRep*0.1);
		$ecartfortRep = (100 - $moyenneRep) / 2;	
		$ecartmidRep = $moyenneRep-$ecartfaibleRep-$ecartfaibleRep;	
	}

	$moyenneRap = number_format($txRapidite,2);;
	if($moyenneRap < 50)
	{
		$ecartfortRap = ($moyenneRap * 2) - ($moyenneRap*0.1);
		$ecartfaibleRap = $moyenneRap / 2;
		$ecartmidRap = 100-$ecartfortRap-$ecartfortRap-$moyenneRap;	
	} else {
		$ecartfaibleRap = ($moyenneRap / 2) - ($moyenneRap*0.1);
		$ecartfortRap = (100 - $moyenneRap) / 2;	
		$ecartmidRap = $moyenneRap-$ecartfaibleRap-$ecartfaibleRap;	
	}
?>		
		ds.push ([[<?php echo $ecartfaibleRap; ?>, 1.5], [<?php echo $ecartfaibleRep; ?>, 3]]);
		ds.push ([[<?php echo $ecartfaibleRap; ?>, 1.5], [<?php echo $ecartfaibleRep; ?>, 3]]);
		ds.push ([[<?php echo $ecartmidRap; ?>, 1.5], [<?php echo $ecartmidRep; ?>, 3]]);
		ds.push ([[<?php echo $ecartfortRap; ?>, 1.5], [<?php echo $ecartfortRep; ?>, 3]]);
		ds.push ([[<?php echo $ecartfortRap; ?>, 1.5], [<?php echo $ecartfortRep; ?>, 3]]);
		ds.push ([[<?php echo number_format(($reponseRepondu/$exercicemanager->getNbQuestion($ex))*100,2); ?>, 0], [<?php echo number_format($rapidite,2); ?>, 1], [<?php echo number_format(($reponseJuste/$exercicemanager->getNbReponseJuste($ex))*100,2); ?>, 2.5]]);
	
		data = [ {
			label: 'Faible',
			data: ds[0],
			color: 'red'
		}, {
			label: 'Moyen',
			data: ds[1],
			color: 'orange'
		}, {
			label: 'Assez bien',
			data: ds[2],
			color: 'blue'
		}, {
			label: 'Bien',
			data: ds[3],
			color: 'green'
		}, {
			label: 'Très bien',
			data: ds[4],
			color: 'yellow'
		}, {
			label: 'Résultat',
			data: ds[5],
			color: 'black'
		}];
	
		chartOptions = {
			xaxis: {
				ticks: [<?php for($i = 0; $i <= 100; $i=$i+5){ echo "[".$i.", '".$i."%'],"; } ?>]
			},
			yaxis: {
				ticks: [[0, "Question traitée"], [1.5, "Temps restant"], [3, "Réponse juste"]]
			},
			grid: {
				hoverable: true,
				clickable: false,
				borderWidth: 0
			},
			legend: {
				//position: "sw",
				container: "#legend-<?php echo $ex; ?>"
  			},
			series: {
				stack: true
			},
			bars: {
				horizontal: true,
				show: true,
				barWidth: .25,
				fill: true,
				lineWidth: 0,
				fillColor: { colors: [ { opacity: 1 }, { opacity: 1 } ] }
			},
			
			tooltip: true,
	
			tooltipOpts: {
				content: '%s: %x'
			},
			colors: target_admin.layoutColors
		}
	
	
		
		var holder = $('#stacked-horizontal-chart-<?php echo $ex; ?>');
	
		if (holder.length) {
			$.plot(holder, data, chartOptions );
		}
	
	
	
	
	});
  </script>
<?php } } ?>

</body>
</html>